<template>
  <div class="box">
    <h1>个人信息</h1>
    <div>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
      >
        <el-form-item label="账号" prop="userName">
          <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="userPwd">
          <el-input v-model="ruleForm.password" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="nikeName">
          <el-input v-model="ruleForm.nikeName" />
        </el-form-item>
        <el-form-item label="手机号" prop="userPhone">
          <el-input v-model="ruleForm.userPhone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="userEmail">
          <el-input v-model="ruleForm.userEmail" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            Create
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { UserReg } from "../api/api";
import { useRouter } from "vue-router";
import { getUser } from "../api/api";

const router = useRouter();

interface RuleForm {
  username: string;
  password: string;
  nikeName: string;
  userPhone: string;
  userEmail: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  username: "",
  password: "",
  nikeName: "",
  userPhone: "",
  userEmail: "",
});

const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: "请输入账号", trigger: "blur" },
    { min: 5, max: 13, message: "账号长度必须是 5-13 位", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    {
      pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,}$/,
      message: "长度至少 8 位，密码必须包含数字字母以及大写字母",
      trigger: "blur",
    },
  ],
  nikeName: [
    { required: true, message: "请输入真实姓名", trigger: "blur" },
    { pattern: /^[\u4e00-\u9fa5]+$/, message: "必须是中文", trigger: "blur" },
  ],
  userPhone: [
    { required: true, message: "请输入手机号", trigger: "blur" },
    {
      pattern:
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      message: "中国大陆正常手机号",
      trigger: "blur",
    },
  ],
  userEmail: [
    { required: true, message: "请输入邮箱", trigger: "blur" },
    {
      pattern: /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
      message: "错误的邮箱",
      trigger: "blur",
    },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      UserReg(ruleForm).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("注册成功！");
          router.push("/login");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

onMounted(() => {
  getUser().then((res) => {
    if (res.data.code == 200) {
      //数据回显
      Object.assign(ruleForm, res.data.data);
    }
  });
});
</script>
